package com.qdxwx.manage.controller.course;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.course.CourseAlbumService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.course.CourseAlbum;
import com.qdxwx.models.enums.BusinessType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 课程专集Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/course/album")
public class CourseAlbumController extends BaseController {
    private final String prefix = "course";

    @Autowired
    private CourseAlbumService courseAlbumService;

    @GetMapping("/list.html")
    public String album() {
        return prefix + "/album-list";
    }

    /**
     * 查询课程专集列表
     */
    @Permissions("course:album")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        CourseAlbum courseAlbum = new CourseAlbum();
        List<CourseAlbum> list = courseAlbumService.getList(courseAlbum, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(courseAlbumService.count(courseAlbum));
        return rspData;
    }

    /**
     * 新增课程专集
     */
    @GetMapping("/add.html")
    public String add(ModelMap mmap) {
        mmap.put("courseAlbum", new CourseAlbum());
        return prefix + "/album-add";
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") int id, ModelMap mmap) {
        if(id < 1)return error404;
        CourseAlbum album = courseAlbumService.get(id);
        if (album == null) return error404;
        mmap.put("courseAlbum", album);
        return prefix + "/album-edit";
    }

    /**
     * 修改排序
     */
    @Permissions("course:album:edit")
    @Log(title = "修改专集", businessType = BusinessType.DELETE)
    @PostMapping("/save.json")
    @ResponseBody
    public AjaxResult saveAlbum(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        CourseAlbum album;
        if (id > 0) {
            album = courseAlbumService.get(id);
            if (album == null) {
                return error("找不到指定专辑");
            }
        } else {
            album = new CourseAlbum();
        }
        album.setName(request.getParameter("name"));
        album.setDescription(request.getParameter("description"));
        album.setLink(request.getParameter("link"));
        album.setCourseNum(TKConvert.toInteger(request.getParameter("courseNum")));
        if (id > 0) {
            return toAjax(courseAlbumService.update(album));
        } else {
            return toAjax(courseAlbumService.insert(album));
        }
    }

    /**
     * 删除课程专集
     */
    @Log(title = "删除专集", businessType = BusinessType.DELETE)
    @Permissions("course:album:remove")
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("ids"));
        if(id < 1) return error("参数id错误,value="+id);
        return toAjax(courseAlbumService.delete(id));
    }
}
